<template>
  <div class="MaterialsReplaceDialog-box">
    <div class="preview-box" ref="previewRef" v-show="store.replaceMaterialsShow && previewShow">
      <img :src="previewUrl" alt="" />
    </div>
    <t-drawer
      v-model:visible="store.replaceSceneShow"
      :confirm-on-enter="true"
      :on-cancel="onCancel"
      :on-esc-keydown="onEscKeydown"
      :on-close-btn-click="onCloseBtnClick"
      :on-overlay-click="onOverlayClick"
      :on-close="close"
      :on-confirm="onConfirmAnother"
      style="height: calc(100% - 48px)"
    >
      <div class="sort-box" @click="previewShow = false">
        <div class="heard-box">
          <span>更换背景</span>
          <i class="doorIcon" @click="store.replaceSceneShow = false">&#xe63d;</i>
        </div>

        <div class="materials-box">
          <div class="conten-style-box">
            <p class="ascription-title">本场景背景</p>
            <div class="ascription-list">
              <MaterialsItem
                @click="setBackground(item)"
                @previewEmit="previewEmit"
                @previewLeave="previewLeave"
                :info="item"
                v-for="item in correlationList"
                :key="item.id"
                :Active="
                  store.styleInfo.backgroundImage === item ||
                  (!store.styleInfo.backgroundImage && oldBackground == item)
                "
              />  
            </div>
            <p class="ascription-title" v-if="NocorrelationList.length > 0">所有背景</p>
            <div class="ascription-list no-ascription-list">
              <MaterialsItem 
                @click="setBackground(item)"
                @previewEmit="previewEmit"
                @previewLeave="previewLeave"
                :info="item"
                v-for="item in NocorrelationList"
                :key="item.id"
                :Active="
                  store.styleInfo.backgroundImage === item ||
                  (!store.styleInfo.backgroundImage && oldBackground == item)
                "
              />
            </div>
          </div>
        </div>

        <div class="footer-box">
          <button class="set-all" @click="reset">
            <span>重置背景</span> 
            <t-tooltip> 
              <template #content>
                <p class="tooltip-content">点击按钮,将会背景重置至默认</p>
              </template>
              <i class="doorIcon">&#xe635;</i>
            </t-tooltip>
          </button> 
        </div>
      </div>
    </t-drawer>
  </div>
</template> 
<script setup lang="ts">
import MaterialsItem from './item.vue';
import { publicUrls } from '@/utils/minxin';
import { MessagePlugin } from 'tdesign-vue-next';
import { SetspecifyfreeExhibitStyle, restshowpieceListStyle, getOriginallyBg } from '@/material/index';
import { getMOdeListArr } from '@/utils/getRemComputed';
import { ref, watch, onMounted, nextTick, computed } from 'vue';
import { useProjectStore, commonDataStore } from '@/store';
import { getBackgroundImageList } from '@/material/index';

const commonStore = commonDataStore();
const webUrls: string = '?imageView2/3/format/webp';
const tabList = ref([]);
const previewRef = ref();
const ActiveType = ref(''); //当前选中的展品素材类型
const store = useProjectStore();
const ActiveIId = ref(0);
const correlationList = ref([]); //关联素材列表
const NocorrelationList = ref([]); //非关联素材列表
const previewUrl = ref('');
const previewShow = ref(false);
const onConfirmAnother = (context) => {
  store.replaceSceneShow = false;
};
const close = (context) => {
  store.replaceSceneShow = false;
};
const oldBackground = ref('');
const onCancel = (context) => {};
const onEscKeydown = (context) => {};
const onCloseBtnClick = (context) => {};
const onOverlayClick = (context) => {};
const init = () => {
  let arr = getBackgroundImageList();
  correlationList.value = arr.correlationList;
  NocorrelationList.value = arr.NocorrelationList;
  oldBackground.value = getOriginallyBg(commonStore.templateId);
};
const previewEmit = (e: string, heights: number) => {
  previewRef.value.style.top = heights + 'px';
  previewUrl.value = e;
  previewShow.value = true;
};
const previewLeave = () => {
  previewShow.value = false;
};
const setBackground = (url) => {
  store.setStyleInfoBackground(url);
};
const reset = () => {
  store.setStyleInfoBackground();
};
watch(
  () => store.replaceSceneShow,
  () => {
    init();
  },
  { deep: true },
);
</script>

<style scoped lang="less">
.sort-box {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
:deep(.t-drawer__header) {
  display: none;
}
:deep(.t-drawer__footer) {
  display: none;
}
:deep(.t-drawer__body) {
  padding: 0px;
  overflow: visible;
}
:deep(.t-drawer__content-wrapper) {
  width: 348px !important;
}
// :deep(.t-drawer--open) {
//   height: calc(100% - 34px);
// }
:deep(.t-drawer--right) {
  top: auto !important;
  bottom: 0px !important;
}
.heard-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 48px;
  background: #f5f5f5;
  padding: 0 24px;
  span {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: rgba(0, 0, 0, 0.59);
    line-height: 28px;
  }
  .doorIcon {
    cursor: pointer;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.59);
  }
}

.footer-box {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  height: 48px;
  padding: 8px 24px;
  .set-all {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 32px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    background: transparent;
    span {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.8);
      line-height: 24px;
    }
    .doorIcon {
      font-size: 16px;
      color: rgba(0, 0, 0, 0.59);
      margin-left: 4px;
    }
  }
}
.materials-box {
  display: flex;
  flex-direction: column;
  padding: 0px 24px;

  width: 100%;
  height: calc(100vh - 140px);
  // overflow: auto;
  scrollbar-width: none;
}
.conten-style-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: auto;
  scrollbar-width: none;
}
.ascription-title {
  margin-top: 16px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.8);
  line-height: 24px;
}
.ascription-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 100%;
  box-shadow: inset 0px -1px 0px 0px #f0f0f0;
  padding-bottom: 16px;
}
.no-ascription-list {
  box-shadow: none;
  padding-bottom: 16px;
}
.item-img {
  width: 100%;
  height: 180px;
  img {
    width: 100%;
    height: 160px;
  }
}
.preview-box {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  top: calc(50% - 160px);
  right: 340px;
  width: 320px;
  height: 320px;
  background: #f0f0f0;
  box-shadow: 0px 12px 12px -7px rgba(0, 0, 0, 0.1), 0px 19px 24px 2px rgba(0, 0, 0, 0.06),
    0px 7px 33px 5px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  border: 1px solid #e6e6e6;
  z-index: 99999;
  img {
    object-fit: contain;
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
  }
}
.tab-list {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 48px;
  box-shadow: inset 0px -1px 0px 0px #f0f0f0;
}
.tab-item {
  cursor: pointer;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.59);
  line-height: 24px;
  width: 32px;
}
.tab-item-active {
  color: #e62b17;
}
.tab-solider {
  transition: all 0.3s;
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 32px;
  height: 2px;
  background: #e62b17;
  border-radius: 1px;
}
.tab-solider-1 {
  left: 50%;
  transform: translateX(-50%);
}
.tab-solider-2 {
  left: 100%;
  transform: translateX(-100%);
}
.tooltip-content {
  width: 198px;
}
:deep(.t-drawer__mask) {
  background: transparent;
}
</style>